#include "moverdownstatus.h"

MoverDownStatus::MoverDownStatus(QWidget *parent) : QWidget(parent)
{

}

MoverDownStatus::MoverDownStatus(const QString &Name, const QJsonObject &functionDetails, QWidget *parent)
    : QWidget(parent)
{
    QGroupBox *groupBox = new QGroupBox("下mover", this);
    groupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    createMoverDownStatus(groupBox, functionDetails);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(groupBox);
    layout->setContentsMargins(0, 0, 0, 0);
    qDebug() << "测试是否进入下mover状态";

    setLayout(layout);
}

void MoverDownStatus::createMoverDownStatus(QGroupBox *groupBox, const QJsonObject &functionDetails)
{
    qDebug() << "functionDetails :" << functionDetails;
    QVBoxLayout *mainLayout = new QVBoxLayout();
    groupBox->setLayout(mainLayout);

    // 创建提升缸部分
    QWidget *LiftingcylinderWidget = new QWidget(groupBox);
    QVBoxLayout *LiftingcylinderLayout = new QVBoxLayout(LiftingcylinderWidget);
    LiftingcylinderWidget->setLayout(LiftingcylinderLayout);
    LiftingcylinderWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QJsonObject liftingcylinders = functionDetails["Liftingcylinder"].toObject();
    for (const QString &cylinderName : liftingcylinders.keys()) {
        QJsonObject cylinder = liftingcylinders[cylinderName].toObject();

        // 创建每个提升缸的QGroupBox
        QGroupBox *cylinderGroupBox = new QGroupBox("举升缸 " + cylinderName.right(1), LiftingcylinderWidget);
        QHBoxLayout *cylinderLayout = new QHBoxLayout(cylinderGroupBox);

        // 创建"状态" QLabel 和 QLineEdit
        QLabel *stateLabel = new QLabel("状态:", cylinderGroupBox);
        QLineEdit *stateEdit = new QLineEdit(cylinder["liftState"].toString(), cylinderGroupBox);
        stateEdit->setReadOnly(true);  // 设置为只读
        cylinderLayout->addItem(new QSpacerItem(20, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)); // 添加弹簧
        cylinderLayout->addWidget(stateLabel);
        cylinderLayout->addItem(new QSpacerItem(20, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)); // 添加弹簧
        cylinderLayout->addWidget(stateEdit);
        cylinderLayout->addItem(new QSpacerItem(20, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)); // 添加弹簧


        // 创建"距离(mm)" QLabel 和 QLineEdit
        QLabel *distanceLabel = new QLabel("位移(mm):", cylinderGroupBox);
        QLineEdit *distanceEdit = new QLineEdit(QString::number(cylinder["liftDistance"].toInt()), cylinderGroupBox);
        distanceEdit->setReadOnly(true);  // 设置为只读
        cylinderLayout->addWidget(distanceLabel);
        cylinderLayout->addItem(new QSpacerItem(20, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)); // 添加弹簧
        cylinderLayout->addWidget(distanceEdit);

        cylinderLayout->addItem(new QSpacerItem(20, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)); // 添加弹簧

        // 创建"举升力(N)" QLabel 和 QLineEdit
        QLabel *forceLabel = new QLabel("举升力(N):", cylinderGroupBox);
        QLineEdit *forceEdit = new QLineEdit(QString::number(cylinder["liftForce"].toInt()), cylinderGroupBox);
        forceEdit->setReadOnly(true);  // 设置为只读

        cylinderLayout->addWidget(forceLabel);
        cylinderLayout->addItem(new QSpacerItem(20, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)); // 添加弹簧
        cylinderLayout->addWidget(forceEdit);
        cylinderLayout->addItem(new QSpacerItem(20, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)); // 添加弹簧

        // 将每个提升缸的 QGroupBox 添加到 LiftingcylinderLayout
        LiftingcylinderLayout->addWidget(cylinderGroupBox);
    }


    // 创建锁块部分
    QWidget *LockBlockWidget = new QWidget(groupBox);
    QVBoxLayout *LockBlockLayout = new QVBoxLayout(LockBlockWidget);
    LockBlockWidget->setLayout(LockBlockLayout);
    LockBlockWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding); // 设置为根据内容自适应高度

    QJsonObject lockBlocks = functionDetails["LockBlock"].toObject();
    int counter = 0;
    QWidget *blockContainer = nullptr; // 容器，用来放置两组 QLabel 和 QLineEdit

    for (const QString &blockName : lockBlocks.keys()) {
        QJsonObject block = lockBlocks[blockName].toObject();

        if (counter % 2 == 0) {
            // 每两组控件创建一个新的容器
            if (blockContainer) {
                LockBlockLayout->addWidget(blockContainer);  // 添加上一个容器
            }
            blockContainer = new QWidget(LockBlockWidget);  // 创建新的容器
            QHBoxLayout *blockContainerLayout = new QHBoxLayout(blockContainer);
            blockContainer->setLayout(blockContainerLayout);
        }

        // 根据锁块名称判断并创建对应的 QLabel
        QLabel *blockLabel;
        if (blockName == "leftLockBlock") {
            blockLabel = new QLabel("左锁紧块锁销状态：", blockContainer);
        } else if (blockName == "rightLockBlock") {
            blockLabel = new QLabel("右锁紧块锁销状态：", blockContainer);
        } else if (blockName == "leftLiftBlock") {
            blockLabel = new QLabel("左举升块锁销状态：", blockContainer);
        } else if (blockName == "rightLiftBlock") {
            blockLabel = new QLabel("右举升块锁销状态：", blockContainer);
        } else {
            blockLabel = new QLabel(blockName, blockContainer);  // 默认标签
        }

        // 创建状态 QLabel 和 QLineEdit
        QLineEdit *stateEdit = new QLineEdit(block["state"].toString(), blockContainer);
        stateEdit->setReadOnly(true);  // 设置为只读

        blockContainer->layout()->addWidget(blockLabel);
        blockContainer->layout()->addItem(new QSpacerItem(20, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)); // 添加弹簧
        blockContainer->layout()->addWidget(stateEdit);
        blockContainer->layout()->addItem(new QSpacerItem(20, 0, QSizePolicy::Expanding, QSizePolicy::Minimum)); // 添加弹簧
        counter++;

        // 如果是最后一组控件，也要将容器添加到布局中
        if (counter % 2 == 0 || counter == lockBlocks.size()) {
            LockBlockLayout->addWidget(blockContainer);
        }
    }

    // 将提升缸和锁块部分添加到主布局
    mainLayout->addWidget(LiftingcylinderWidget);
    mainLayout->addWidget(LockBlockWidget);
}
